package action.weeklog;

import java.util.ArrayList;
import java.util.List;

import model.Page;
import model.User;
import model.Weeklog;
import model.WeeklogMessage;
import DAO.WeeklogDAO;
import DAO.DAOImple.WeeklogImple;

import com.opensymphony.xwork2.ActionSupport;

public class WeeklogAction extends ActionSupport {
	
	private User user;
	private List<User> users;
	private Weeklog weekLog;
	private Page page;
	private List<Weeklog> weekLogList = new ArrayList<Weeklog>();
	private int messager;
	private int messageFor;
	private WeeklogMessage weeklogMessage;
	private List<WeeklogMessage> weeklogMessageList = new ArrayList<WeeklogMessage>();
	
	public String execute() {
		return SUCCESS;
	}
	public String publish() {
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_WeekLog where state = 0", page.getCurrentPage(), 10);
		
		WeeklogDAO weeklogDao = new WeeklogImple();
		int result = weeklogDao.addWeeklog(weekLog);
		if(result == 0) {
			System.out.println("增加失败");
		}else {
			System.out.println("增加成功");
		}
		
		System.out.println(weekLog.getUser().getId());
		weekLogList = weeklogDao.getWeeklogList(weekLog.getUser().getId(), page);
		return SUCCESS;
	}
	
	public String myWeekLog() {
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_WeekLog where state = 0", page.getCurrentPage(), 10);
		
		WeeklogDAO weeklogDao = new WeeklogImple();
		weekLogList = weeklogDao.getWeeklogList(weekLog.getUser().getId(), page);
		
		return SUCCESS;
	}
	
	
	public String byUsers() {
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_WeekLog where state = 0", page.getCurrentPage(), 10);
		
		WeeklogDAO weeklogDao = new WeeklogImple();
		weekLogList = weeklogDao.getWeeklogByUser(user, page);
		
		return SUCCESS;
	}
	
	public String info() {
		
		WeeklogDAO weeklogDao = new WeeklogImple();
		weekLog = weeklogDao.getWeeklogById(weekLog.getId());
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_WeeklogMessage where state = 0",page.getCurrentPage(),10);  //获得分页对象
		
		weeklogMessageList = weeklogDao.getWeeklogMessage(weekLog.getId(), page);
		return SUCCESS;
	}
	
	/*
	 * 增加周报留言
	 */
	public String addMessage() {
		WeeklogDAO weeklogDao = new WeeklogImple();
		int result = weeklogDao.addWeeklogMessage(weeklogMessage);
		if(result == 0) {
			System.out.println("增加失败");
		} else {
			System.out.println("增加成功");
		}
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_WeeklogMessage where state = 0",page.getCurrentPage(),10);  //获得分页对象
		
		
		weekLog = weeklogDao.getWeeklogById(weeklogMessage.getMessageFor().getId());
		weeklogMessageList = weeklogDao.getWeeklogMessage(weekLog.getId(), page);
		return SUCCESS;
	}

	/*
	 * 根据周报留言者和周报发布者得到周报留言
	 */
	public String getMessage() {
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_WeeklogMessage where state = 0",page.getCurrentPage(),10);  //获得分页对象
		
		WeeklogDAO weeklogDao = new WeeklogImple();
		weeklogMessageList = weeklogDao.getWeeklogMessage(weekLog.getId(), page);
		
		System.out.println(weeklogMessageList);
		
		return SUCCESS;
	}
	
	/*
	 * 根据周报留言的id删除周报留言
	 */
	public String deleteMessage() {
		
		WeeklogDAO weeklogDao = new WeeklogImple();
		weeklogDao.deleteWeeklogMessage(weeklogMessage.getId());
		
		return SUCCESS;
	}
	
	
	public String teammates() {
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public Weeklog getWeekLog() {
		return weekLog;
	}

	public void setWeekLog(Weeklog weekLog) {
		this.weekLog = weekLog;
	}

	public List<Weeklog> getWeekLogList() {
		return weekLogList;
	}

	public void setWeekLogList(List<Weeklog> weekLogList) {
		this.weekLogList = weekLogList;
	}
	
	public WeeklogMessage getWeeklogMessage() {
		return weeklogMessage;
	}

	public void setWeeklogMessage(WeeklogMessage weeklogMessage) {
		this.weeklogMessage = weeklogMessage;
	}

	public List<WeeklogMessage> getWeeklogMessageList() {
		return weeklogMessageList;
	}

	public void setWeeklogMessageList(List<WeeklogMessage> weeklogMessageList) {
		this.weeklogMessageList = weeklogMessageList;
	}

	public int getMessager() {
		return messager;
	}

	public void setMessager(int messager) {
		this.messager = messager;
	}

	public int getMessageFor() {
		return messageFor;
	}

	public void setMessageFor(int messageFor) {
		this.messageFor = messageFor;
	}
	
}
